const api = require('../../../../ws_micro_shop/utils/api.js');
const app = getApp();

Page({
  data: {
    userInfo: {},
    time: '',
    theme: app.themeData,
    today: '',
  },
  onLoad() {
    let userInfo = wx.getStorageSync('user_info');
    this.setData({
      userInfo,
      today: new Date().toLocaleDateString().split('/').join('-'),
      time: userInfo.birthday,
    });
  },
  pickDate(e) {
    this.setData({
      time: e.detail.value,
    });
  },
  submit(e) {
    let { userInfo, time } = this.data;
    let { name: realname, sex: gender } = e.detail.value;
    app.ws_micro_shoprequest({
      url: api.user.edit_info,
      data: {
        gender,
        realname,
        day: time,
      },
      method: 'POST',
      success: function (res) {
        if (res.code === 0) {
          userInfo.realname = realname;
          userInfo.gender = gender;
          userInfo.birthday = time;
          wx.setStorageSync('user_info', userInfo);
          wx.showModal({
            title: '提示',
            content: '修改成功',
            showCancel: false,
            success: t => {
              if (t.confirm) {
                wx.navigateBack();
              }
            },
          })
        } else {
          wx.showToast({
            title: '修改失败',
            icon: 'none',
          });
        }
      },
    });
  },
})